<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class Admin_userController extends Controller
{

     //管理员列表
    public function getIndex(Request $request){

        //获取搜索id
        $search = $request->input('search');

        //获取页数id
        $num = $request->input('num',10);

        //判断搜索   模糊查询
        if(!empty($search)){
            $data = DB::table('admin_user')->where('a_uname','like','%'.$search.'%')->paginate($num);
        }else{
            //获取列表页信息
            $data = DB::table('admin_user')->paginate($num);
        }

        //引入模板传输数据
        return view('admin.admin_user.index',['data'=>$data,'request'=>$request->all()]);
    }

    //管理员添加页
    public function getAdd(){
        return view('admin.admin_user.add');
    }

    //管理员插入操作
    public function postInsert(Request $request){
        //压入空数组
        $arr = array();

        //去除 _token
        $arr = $request->except('_token');

        //哈希解密
        $arr['a_upassword'] = Hash::make($arr['a_upassword']);

        //验证数据
        $this->validate($request,[
            'a_uname' => 'required',
            'a_upassword' => 'required',
        ],[
            'a_uname.required' => '管理员登陆账号必填',
            'a_upassword.required' => '管理员登陆密码必填',
        ]);

        //获取插入的数据
        $res = DB::table('admin_user')->insert($arr);

        //判断结果
        if($res){
            return redirect('/admin/admin_user/index')->with('success','插入成功');
        }else{
            return back()->with('error','插入失败');
        }
    }


    //管理员修改页
    public function getEdit(Request $request){

        //获取修改id
        $res = $request->input('a_uid');

        //获取要修改的一条完整信息
        $data = DB::table('admin_user')->where('a_uid',$res)->get();
        
        //引入模板  传输数据
        return view('admin.admin_user.edit',['data'=>$data[0]]);
    }

    //管理员修改操作
    public function postUpdate(Request $request){

        //获取修改的id
        $res = $request->input('a_uid');

        //创建空数组
        $data = array();

        //去除_token其余数据
        $data = $request->except('_token');

        //哈希解密
        $data['a_upassword'] = Hash::make($data['a_upassword']);

        //获取修改的数据
        $res1 = DB::table('admin_user')->where('a_uid',$res)->update($data);

        //判断结果
        if($res1>0){
            return redirect('admin/admin_user/index')->with('success','管理员信息修改成功');
        }else{
            return back()->with('error','管理员信息修改失败');
        }
    }

    //管理员删除操作
    public function getDelete(Request $request){

        //获取删除id
        $res = $request->input('a_uid');

        //获取要删除的某条数据
        $data = DB::table('admin_user')->where('a_uid',$res)->delete();

        //判断结果
        if($data>0){
            return redirect('admin/admin_user/index')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    //管理员升权
    public function getUp(Request $request){

        //获取升权id
        $res = $request->input('a_uid');

        //获取升权的数据
        $data = DB::table('admin_user')->where('a_uid',$res)->first();

        //判断结果
        if($data['a_uauth']==1){
            session(['data.a_uauth'=>2]);
            $res = DB::table('admin_user')->where('a_uid',$res)->update(['a_uauth'=>2]);  
            return back()->with('success','升权成功');
        }else{
            return back()->with('error','升权失败');
        }
            
    }
    

    //管理员降权
    public function getDown(Request $request){

        //获取降权id
        $res = $request->input('a_uid');

        //获取降权数据
        $data = DB::table('admin_user')->where('a_uid',$res)->first();

        //判断结果
        if($data['a_uauth']==2){
            session(['data.a_uauth'=>1]);
            // dd(session()->all());
            $res = DB::table('admin_user')->where('a_uid',$res)->update(['a_uauth'=>1]);       
           return redirect('/admin/goods/index')->with('success','降权成功'); 
        }else{
            return back()->with('error','降权失败');
        }
    }
}    
